<?php
/**
 * 表数据模型
 */
class core_m_follow extends core_base_m {
	
	public function tableName() {
		return core_base_Constant::TABLE_PREFIX . 'follow';
	}
	/**
	 * 获取用户的所有关注列表
	 * @param bigint $fans_uid
	 * @param int $page
	 * @param int $limit
	 */
	function getFriendList($fans_uid, $page=1, $limit=10){
		$this->setPage($page);
		$this->setLimit($limit);
		$this->setCount(TRUE);
		$rs = $this->select("fans_uid={$fans_uid}",'uid','',"order by dateline desc");
		$rsList['count'] = $rs->totalSize;
		$rsList['list'] = $rs->items;
		return $rsList;
	}
	/**
	 * 获取用户关注数
	 * @param bigint $fans_uid
	 */
	function getFriendCount($fans_uid){
		$rs = $this->selectOne("fans_uid={$fans_uid}",'count(1) as num');
		return $rs['num'];
	}
	/**
	 * 获取用户的粉丝列表
	 * @param bigint $uid
	 * @param int $page
	 * @param int $limit
	 */
	function getFollowList($uid, $page, $limit){
		$this->setPage($page);
		$this->setLimit($limit);
		$this->setCount(TRUE);
		$rs = $this->select("uid={$uid}",'fans_uid','',"order by dateline desc");
		$rsList['count'] = $rs->totalSize;
		$rsList['list'] = $rs->items;
		return $rsList;
	}
	/**
	 * 获取用户粉丝数
	 * @param bigint $uid
	 */
	function getFollowCount($uid){
		$rs = $this->selectOne("uid={$uid}",'count(1) as num');
		return $rs['num'];
	}
	/**
	 * 增加用户关注关系$fans_uid关注$uid
	 * @param bigint $uid
	 * @param bigint $fans_uid
	 */
	function addFollow($uid, $fans_uid){
		if($uid and $fans_uid){
			$data = array('uid'=>$uid, 'fans_uid'=>$fans_uid, 'datetime'=>$this->_time);
			if($this->insert($data)==false){
				$this->setError(0,"关注出错，你已经关注过该用户！");
				return false;
			}else{
				$mUser = core_base_mAPI::get("core_m_user");
				$fans = $this->getFollowCount($uid);//被关注者的粉丝数
				$follow = $this->getFriendCount($fans_uid);//自己关注数量
				$mUser->update("uid={$fans_uid}",$follow);//更新自己的关注数
				$mUser->update("uid={$uid}",$fans);//更新对方的粉丝数
				return true;
			}
		}
		return false;
	}
	/**
	 * 批量增加用户关注关系
	 * @param bigint $uid
	 * @param array $fanUidList
	 */
	function addFollowBatch($uid, $fanUidList){
		if ( $uid && is_array($fanUidList) && !empty($fanUidList) ){
			$dateline 	= $this->_time;
			$values		= array();
			$fields		= '(uid, fans_uid, dateline)';
			foreach ($fanUidList as $aFanUid) {
				$values[] = "('$aFanUid', '$uid', '$dateline')";
			}
			$valueStr = implode(',', $values);
			if ($valueStr){
				$table = $this->tableName();
				$sql = "Insert Into {$table} $fields values $valueStr";
				$this->execute($sql);
				return count($values);
			}
		}
		return false;
	}
	/**
	 * 删除用户关注关系
	 * @param bigint $uid
	 * @param bigint $fans_uid
	 */
	function delFollow($uid, $fans_uid){
		if($uid and $fans_uid){
			$this->delete("uid={$uid} and fans_uid={$fans_uid}");
			return true;
		}
		return false;
	}
	/**
	 * 删除某用户的所有关注
	 * @param bigint $fans_uid
	 */
	function delAllFriend($fans_uid){
		if($fans_uid){
			$this->delete("fans_uid={$fans_uid}");
			return true;
		}
		return false;
	}
	/**
	 * 判断$uid 是否为$fans_uid的关注用户
	 * @param bigint $uid
	 * @param bigint $fans_uid
	 */
	function isFriendShip($uid, $fans_uid){
		if($uid and $fans_uid){
			return (bool)$this->get("uid={$uid} and fans_uid={$fans_uid}");
		}
		return false;
	}
}